package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.worker.SalaryFa;
import com.sand.dto.worker.SelectSalary;
import com.sand.entity.work.Salary;
import com.sand.entity.work.Salary_Details;
import com.sand.mapper.SalaryMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.service.SalaryDetailsService;
import com.sand.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements SalaryService {

    @Autowired
    private SalaryDetailsService salaryDetailsService;


    /**
     * 发放工资
     *
     * @param salaryFa
     */
    @Override
    @Transactional
    public Integer fa(SalaryFa salaryFa) {

        Integer merchantid = salaryFa.getMerchantid();

        Integer workerid = salaryFa.getWorkerid();

        Integer projectid = salaryFa.getProjectid();

        Integer money = salaryFa.getMoney();

        if (merchantid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入发工资的人");
        }

        if (workerid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入接收工资的人");
        }

        if (projectid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入发工资的工程");
        }

        if (money == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入要发工资金额");
        }

        //设置发工资时间
        salaryFa.setTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        //获取工资表
        Salary salary = getOne(new LambdaQueryWrapper<Salary>().eq(Salary::getProjectid, projectid).eq(Salary::getWorkerid, workerid));

        if (salary == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"当前接收发工资的人不存在这个工程中");
        }

        //存入发工资的操作
        Salary_Details salaryDetails = new Salary_Details();

        salaryDetails.setTime(salaryFa.getTime());
        salaryDetails.setMerchantid(merchantid);
        salaryDetails.setWorkerid(workerid);
        salaryDetails.setProjectid(projectid);
        salaryDetails.setMoney(money);

        //添加
        salaryDetailsService.save(salaryDetails);

        //修改工资表信息
        salary.setSsalary(money);

        if (salary.getYsalary() >= salary.getSsalary()){
            //结清
            salary.setWhether(1);
        }

        //修改
        updateById(salary);

        return 1;
    }

    /**
     * 条件查询工人的工资情况
     *
     * @param selectSalary
     */
    @Override
    public List<Salary> all(SelectSalary selectSalary) {
        LambdaQueryWrapper<Salary> wrapper = new LambdaQueryWrapper<>();

        if (selectSalary.getProjectid() != null){
            wrapper.eq(Salary::getProjectid,selectSalary.getProjectid());
        }

        if (selectSalary.getWorkerid() != null){
            wrapper.eq(Salary::getWorkerid,selectSalary.getWorkerid());
        }

        if (selectSalary.getWhether() != null){
            wrapper.eq(Salary::getWhether,selectSalary.getWhether());
        }

        return list(wrapper);
    }


}
